package calcs;

import gui.data.others.ConverterGui;

import javax.swing.JLabel;
import javax.swing.JTextField;
import static util.Util.*;

public class ConverterCalculator implements Runnable {

	int from = 1, to = 1, guide = 1;
	Double in, res = 0.0;
	JLabel output;
	JTextField input;
	boolean error = false;

	public ConverterCalculator(JTextField input, JLabel output) {
		this.input = input;
		this.output = output;
	}

	public void setFrom(int i) {
		from = i;
	}

	public void setTo(int i) {
		to = i;
	}

	public void setGuide(int i) {
		guide = i;
	}

	@Override
	public void run() {
		try {
			in = Double.valueOf(input.getText());
			error = false;
		} catch (NumberFormatException e) {
			error = true;
		}
		if (!error) {
			if (from == to) {
				res = in;
			} else {
				switch (guide) {
				case 1:
					calculateTemp();
					break;
				case 2:
					calculateWeight();
					break;
				case 3:
					calculateLength();
					break;
				case 4:
					calculateArea();
					break;
				case 5:
					calculateVolume();
					break;
				}
			}
			output.setText("" + res);
		} else {
			output.setText("N/A");
		}
	}

	private void calculateVolume() {
		if (from == 1) {
			if (to == 2) {
				res = in*4.54609;
			} else if (to == 3) {
				res = in*8;
			} else if (to == 4) {
				res = in*0.0381254102;
			} else if (to == 5) {
				res = in*4546.09;
			} else if (to == 6) {
				res = in*160;
			}
		} else if (from == 2) {
			if (to == 1) {
				res = in*0.219969;
			} else if (to == 3) {
				res = in*1.75975;
			} else if (to == 4) {
				res = in*0.00838641436;
			} else if (to == 5) {
				res = in*1000;
			} else if (to == 6) {
				res = in*35.1951;
			}
		} else if (from == 3) {
			if (to == 1) {
				res = in*0.125;
			} else if (to == 2) {
				res = in*0.568261;
			} else if (to == 4) {
				res = in*0.00476567628;
			} else if (to == 5) {
				res = in*568.261;
			} else if (to == 6) {
				res = in*20;
			}
		} else if (from == 4) {
			if (to == 1) {
				res = in*26.229226;
			} else if (to == 2) {
				res = in*119.240471;
			} else if (to == 3) {
				res = in*209.833808;
			} else if (to == 5) {
				res = in*119240.471;
			} else if (to == 6) {
				res = in*4196.67616;
			}
		} else if (from == 5) {
			if (to == 1) {
				res = in*0.000219969;
			} else if (to == 2) {
				res = in*0.001;
			} else if (to == 3) {
				res = in*0.00175975;
			} else if (to == 4) {
				res = in*0.00000838641436;
			} else if (to == 6) {
				res = in*0.0351951;
			}
		} else if (from == 6) {
			if (to == 1) {
				res = in*0.00625;
			} else if (to == 2) {
				res = in*0.0284131;
			} else if (to == 3) {
				res = in*0.05;
			} else if (to == 4) {
				res = in*0.000238283814;
			} else if (to == 5) {
				res = in*28.4131;
			}
		}
	}

	private void calculateArea() {
		if (from == 1) {
			if (to == 2) {
				res = in*0.404686;
			} else if (to == 3) {
				res = in*43560;
			} else if (to == 4) {
				res = in*4046.86;
			} else if (to == 5) {
				res = in*0.0015625;
			} else if (to == 6) {
				res = in*6273000;
			} else if (to == 7) {
				res = in*0.00404686;
			}
		} else if (from == 2) {
			if (to == 1) {
				res = in*2.47105;
			} else if (to == 3) {
				res = in*107639;
			} else if (to == 4) {
				res = in*10000;
			} else if (to == 5) {
				res = in*0.00386102;
			} else if (to == 6) {
				res = in*15500000;
			} else if (to == 7) {
				res = in*0.01;
			}
		} else if (from == 3) {
			if (to == 1) {
				res = in*0.000022957;
			} else if (to == 2) {
				res = in*0.0000092903;
			} else if (to == 4) {
				res = in*0.092903;
			} else if (to == 5) {
				res = in*0.00000003587;
			} else if (to == 6) {
				res = in*144;
			} else if (to == 7) {
				res = in*0.000000092903;
			}
		} else if (from == 4) {
			if (to == 1) {
				res = in*0.000247105;
			} else if (to == 2) {
				res = in*0.0001;
			} else if (to == 3) {
				res = in*10.7639;
			} else if (to == 5) {
				res = in*0.0000003861;
			} else if (to == 6) {
				res = in*1550;
			} else if (to == 7) {
				res = in*0.000001;
			}
		} else if (from == 5) {
			if (to == 1) {
				res = in*640;
			} else if (to == 2) {
				res = in*258.999;
			} else if (to == 3) {
				res = in*27880000;
			} else if (to == 4) {
				res = in*2590000;
			} else if (to == 6) {
				res = in*401400000*10;
			} else if (to == 7) {
				res = in*2.58999;
			}
		} else if (from == 6) {
			if (to == 1) {
				res = in*0.00000015942;
			} else if (to == 2) {
				res = in*0.000000064516;
			} else if (to == 3) {
				res = in*0.00694444;
			} else if (to == 4) {
				res = in*0.00064516;
			} else if (to == 5) {
				res = in*0.0000000002491;
			} else if (to == 7) {
				res = in*0.00000000064516;
			}
		} else if (from == 7) {
			if (to == 1) {
				res = in*247.105;
			} else if (to == 2) {
				res = in*100;
			} else if (to == 3) {
				res = in*10760000;
			} else if (to == 4) {
				res = in*1000000;
			} else if (to == 5) {
				res = in*0.386102;
			} else if (to == 6) {
				res = in*1550000000;
			}
		}
	}

	private void calculateLength() {
		if (from == 1) {
			if (to == 2) {
				res = in*1.60934;
			} else if (to == 3) {
				res = in*1609.34;
			} else if (to == 4) {
				res = in*1760;
			} else if (to == 5) {
				res = in*5280;
			} else if (to == 6) {
				res = in*63360;
			} else if (to == 7) {
				res = in*160934;
			}
		} else if (from == 2) {
			if (to == 1) {
				res = in*0.621371;
			} else if (to == 3) {
				res = in*1000;
			} else if (to == 4) {
				res = in*1093.61;
			} else if (to == 5) {
				res = in*3280.84;
			} else if (to == 6) {
				res = in*39370.1;
			} else if (to == 7) {
				res = in*100000;
			}
		} else if (from == 3) {
			if (to == 1) {
				res = in*0.000621371;
			} else if (to == 2) {
				res = in*0.001;
			} else if (to == 4) {
				res = in*1.09361;
			} else if (to == 5) {
				res = in*3.28084;
			} else if (to == 6) {
				res = in*39.3701;
			} else if (to == 7) {
				res = in*100;
			}
		} else if (from == 4) {
			if (to == 1) {
				res = in*0.000568182;
			} else if (to == 2) {
				res = in*0.0009144;
			} else if (to == 3) {
				res = in*0.9144;
			} else if (to == 5) {
				res = in*3;
			} else if (to == 6) {
				res = in*36;
			} else if (to == 7) {
				res = in*91.44;
			}
		} else if (from == 5) {
			if (to == 1) {
				res = in*0.000189394;
			} else if (to == 2) {
				res = in*0.0003048;
			} else if (to == 3) {
				res = in*0.3048;
			} else if (to == 4) {
				res = in/3;
			} else if (to == 6) {
				res = in*12;
			} else if (to == 7) {
				res = in*30.48;
			}
		} else if (from == 6) {
			if (to == 1) {
				res = in*0.000015783;
			} else if (to == 2) {
				res = in*0.0000254;
			} else if (to == 3) {
				res = in*0.0254;
			} else if (to == 4) {
				res = in*0.0277778;
			} else if (to == 5) {
				res = in*0.0833333;
			} else if (to == 7) {
				res = in*2.54;
			}
		} else if (from == 7) {
			if (to == 1) {
				res = in*0.0000062137;
			} else if (to == 2) {
				res = in*0.00001;
			} else if (to == 3) {
				res = in*0.01;
			} else if (to == 4) {
				res = in*0.0109361;
			} else if (to == 5) {
				res = in*0.0328084;
			} else if (to == 6) {
				res = in*0.393701;
			}
		}
	}

	private void calculateWeight() {
		if (from == 1) {
			if (to == 2) {
				res = in * 2.20462;
			} else if (to == 3) {
				res = in * 0.15747;
			} else if (to == 4) {
				res = in * 1000;
			} else if (to == 5) {
				res = in * 35.274;
			} else if (to == 6) {
				res = in * 5000;
			}
		} else if (from == 2) {
			if (to == 1) {
				res = in * 0.45359;
			} else if (to == 3) {
				res = in * 0.071428;
			} else if (to == 4) {
				res = in * 453.592;
			} else if (to == 5) {
				res = in * 16;
			} else if (to == 6) {
				res = in * 2267.96185;
			}
		} else if (from == 3) {
			if (to == 1) {
				res = in*6.35029;
			} else if (to == 2) {
				res = in*14;
			} else if (to == 4) {
				res = in*6350.29;
			} else if (to == 5) {
				res = in*224;
			} else if (to == 6) {
				res = in*31751.4659;
			}
		} else if (from == 4) {
			if (to == 1) {
				res = in*0.001;
			} else if (to == 2) {
				res = in*0.00220462;
			} else if (to == 3) {
				res = in*0.000157473;
			} else if (to == 5) {
				res = in*0.035274;
			} else if (to == 6) {
				res = in*5;
			}
		} else if (from == 5) {
			if (to == 1) {
				res = in*0.0283495;
			} else if (to == 2) {
				res = in*0.0625;
			} else if (to == 3) {
				res = in*0.00446429;
			} else if (to == 4) {
				res = in*28.3495;
			} else if (to == 6) {
				res = in*141.747616;
			}
		} else if (from == 6) {
			if (to == 1) {
				res = in*0.0002;
			} else if (to == 2) {
				res = in*0.000440924524;
			} else if (to == 3) {
				res = in*0.0000314946089;
			} else if (to == 4) {
				res = in*0.2;
			} else if (to == 5) {
				res = in*0.00705479239;
			}
		}
	}

	private void calculateTemp() {
		if (from == 1) {
			if (to == 2) {
				res = ((in * 9) / 5) + 32;
			} else if (to == 3) {
				res = in + 273.15;
			} else if (to == 4) {
				res = ((in + 273.15) * 9) / 5;
			}
		} else if (from == 2) {
			if (to == 1) {
				res = ((in - 32) * 5) / 9;
			} else if (to == 3) {
				res = ((in + 459.67) * 5) / 9;
			} else if (to == 4) {
				res = in + 459.67;
			}
		} else if (from == 3) {
			if (to == 1) {
				res = in - 273.15;
			} else if (to == 2) {
				res = ((in * 9) / 5) - 459.67;
			} else if (to == 4) {
				res = (in * 9) / 5;
			}
		} else if (from == 4) {
			if (to == 1) {
				res = ((in - 491.67) * 5) / 9;
			} else if (to == 2) {
				res = in - 459.67;
			} else if (to == 3) {
				res = (in * 5) / 9;
			}
		}
	}
}
